<?php
namespace Admin\Controller;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RoleController
 *
 * @author John
 */
class RoleController extends AdminController{
    
    public function index(){
        $list = D("Role")->relation("User")->order("id")->select();
        $this->assign("list",$list);
        $this->display();
    }
    
    /*
     * 角色添加
     */
    public function add(){
        $role_db = D("Role");
        if(IS_POST){
            if($model = $role_db->create())
            {
                if($model['id']){
                    $role_db->save();
                    $role_id = $model['id'];
                }else{
                    $role_id = $role_db->add();
                }
                $this->set_node_to_role($role_id, I("post.nodes"));
            }else{
                $this->error($role_db->getError());
            }
        }else{
            /*
            $where['level'] = array('lt',3);
            $node = M("Node")->where($where)->order('sort')->select();
            $node = Tree::create($node);
             * 
             */
            
            $node = array();
            $node = M("Node")->where(array("level" => 2))->order("sort")->select();
            foreach ($node as $key => &$value) {
                $child = M("Node")->where(array("level" => 3,"pid" => $value['id']))->order("sort")->select();
                $value['child'] = $child;
            }
            $this->assign("node",$node);
            $this->display();
        }
    }
    
     /*
     * 角色修改
     */
    public function edit(){
        $role_db = D("Role");
        if(IS_POST){
            if($model = $role_db->create())
            {
                if($model['id']){
                    $role_db->save();
                    $role_id = $model['id'];
                }else{
                    $this->error("找不到该角色");
                }
                $this->set_node_to_role($role_id, I("post.nodes"));
            }else{
                $this->error($role_db->getError());
            }
        }else{
            
            $node = array();
            $node = M("Node")->where(array("level" => 2))->order("sort")->select();
            foreach ($node as $key => &$value) {
                $child = M("Node")->where(array("level" => 3,"pid" => $value['id']))->order("sort")->select();
                $value['child'] = $child;
            }
            
            $id = I("get.id",0,"intval");
            if($id){
                $select_nodes = M("Access")->where(array("role_id" => $id))->getField("node_id",true);
                $select_nodes = implode(",", $select_nodes);
                $role = $role_db->find($id);
                $this->assign("role",$role);
                $this->assign("select_nodes",$select_nodes);
            }else{
                $this->error("找不到该角色");
            }
            $this->assign("node",$node);
            $this->display();
        }
    }
    
    public function del(){
        $ids = I("post.id");
        $role_db = D("Role");
        $ret = $role_db->deleteRole($ids);
        echo $ret;
    }
    
    /*
     * 给角色设置权限
     */
    private function set_node_to_role($role_id,$nodes){
        //先删除，再增加。
        $node_db = D("Node");
        $access_db = D("Access");
        $access_db->where(array("role_id" => $role_id))->delete();
        
        $node_arr = explode(",", $nodes);
        foreach ($node_arr as $node) {
            $node_info = $node_db->find($node);
            //判断上级node是否被选中  没有选中就设为选中
            $pid = $node_info['pid'];
            if($node_info['level']>1 && $pid!=0){
                $has = $access_db->where(array(
                    'node_id' => $pid,
                    'role_id' => $role_id
                ))->find();
                if(!$has){
                    $_data = array(
                        'role_id'   => $role_id,
                        'node_id'   => $pid,
                        'level'     => $node_info['level']-1,
                    );
                $access_db->data($_data)->add();
                }
            }
            //结束
            $data = array(
                'role_id'   => $role_id,
                'node_id'   => $node_info['id'],
                'level'     => $node_info['level'],
            );
            $access_db->data($data)->add();
        }
    }
}
